package com.dc.wifi.main.ui.activity

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.angcyo.tablayout.delegate2.ViewPager2Delegate
import com.dc.netlib.base.BaseActivity
import com.dc.wifi.BR
import com.dc.wifi.R
import com.dc.wifi.databinding.ActivityMainBinding
import com.dc.wifi.main.adapter.BottomTabAdapter
import com.dc.wifi.main.adapter.MainPageAdapter
import com.dc.wifi.main.model.MainViewModel
import com.dc.wifi.main.ui.fragment.HomeFragment
import com.dc.wifi.main.ui.fragment.MallFragment
import com.dc.wifi.main.ui.fragment.MineFragment

class MainActivity :
    BaseActivity<MainViewModel, ActivityMainBinding>(R.layout.activity_main, BR.mMainViewModel) {
    private lateinit var mainPageAdapter: MainPageAdapter

    override fun initData() {
        super.initData()
        val fragmentList =
            listOf<Fragment>(HomeFragment.createFragment(),MallFragment.createFragment(), MineFragment.createFragment())



    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)




        for (i in bottomItemTitle.indices){
            //TODO TabItemBinding.inflate(layoutInflater)) 通过 ViewBinding 的方式，拿到 tab_item.xml 布局
//            TabItemBinding.inflate(layoutInflater).apply {
//                content.text = bottomItemTitle[i]
//                icon.setImageResource(bottomItemIcon[i])
//
//                // 将当前添加了 icon 和 content 的 tab_item 添加到 TabLayout 中
//                binding.dslTabLayout.addView(root)
//            }
            val inflate = View.inflate(this, R.layout.tab_item, null)
            inflate.findViewById<TextView>(R.id.content).text = bottomItemTitle[i]
            inflate.findViewById<ImageView>(R.id.icon).setImageResource(bottomItemIcon[i])
            binding.dslTabLayout.addView(inflate)
        }

        // TODO ２ViewPager + Fragment
//        binding.viewPager.offscreenPageLimit = bottomItemTitle.size
//        binding.viewPager.adapter = BottomTabAdapter(this, bottomItemTitle.size)

        binding.vp2MainContent.offscreenPageLimit=bottomItemTitle.size
        binding.vp2MainContent.adapter=BottomTabAdapter(this,bottomItemTitle.size)

        // TODO　３将 ViewPager 中的 Fragment 与 底部的 Tab 绑定
//        ViewPager2Delegate.install(binding.viewPager, binding.dslTabLayout, false)


        ViewPager2Delegate.install(binding.vp2MainContent, binding.dslTabLayout, false)

    }

    companion object{
        // TODO 底部 item 的标题
        private val bottomItemTitle = listOf(
            "首页",
            "商城",
            "我的",
        )
        // TODO 底部 item 的图标
        private val bottomItemIcon = listOf(
            R.drawable.selector_tab_discovery,
            R.drawable.selector_tab_category,
            R.drawable.selector_tab_me,
            R.drawable.selector_tab_video
            )


    }

    override fun initEvent() {
        super.initEvent()
    }
}